package org.xx.sipahi.jpa;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.xx.sipahi.Presentation;

import java.util.OptionalLong;

import static org.xx.sipahi.jpa.CategoryRepository.toCategory;

public interface PresentationRepository
        extends JpaRepository<PresentationPO, Long>, JpaSpecificationExecutor<PresentationPO> {
    static Presentation toPresentation(
            PresentationPO po
    ) {
        if (po == null) {
            return null;
        } else {
            return new Presentation(
                    po.getId(),
                    po.getTitle(),
                    toCategory(po.getCategory()),
                    po.getKeywords(),
                    po.getBrief(),
                    po.getCreator(),
                    po.getCreateTime(),
                    po.getUpdater(),
                    po.getLastModifiedTime(),
                    po.getStatus(),
                    ""
            );
        }
    }

    static Presentation toPresentation(
            PresentationPO po,
            PresentationContentPO cpo
    ) {
        if (po == null || cpo == null) {
            return null;
        } else {
            return new Presentation(
                    po.getId(),
                    po.getTitle(),
                    toCategory(po.getCategory()),
                    po.getKeywords(),
                    po.getBrief(),
                    po.getCreator(),
                    po.getCreateTime(),
                    po.getUpdater(),
                    po.getLastModifiedTime(),
                    po.getStatus(),
                    cpo.getContent()
            );
        }
    }

    @Query("select id from presentation where category.id = :categoryId and title = 'index'")
    Long findIndex(
            int categoryId
    );
}
